package com.dliu.javadesign.structure.flyweight;

import java.util.HashMap;

/**
 * @author yanglei
 * date 2021/3/31 19:15
 */
public class SecKill {
    public static HashMap<Long, Activity> cache = new HashMap<>();

    public static Activity getActivity(Long id){
        Activity activity = cache.get(id);
        if (activity == null){
            activity = new Activity().setId(1L).setName("活动一").setDesc("庆祝五一放假");
            Stock stock = new Stock().setTotal(120).setUsed(0);
            activity.setStock(stock);
            cache.put(activity.getId(), activity);
        }
        Stock stock = activity.getStock().addUsed(id.intValue());
        activity.setStock(stock);
        return activity;
    }

    public static void main(String[] args) {
        int i = 10;
        while (i-- > 0){
            System.out.println(SecKill.getActivity(1L));
        }
    }
}
